package com.platform.controller;

import com.platform.cache.J2CacheUtils;
import com.platform.entity.ProductCateEntity;
import com.platform.service.ProductCateService;
import com.platform.util.Constants;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Controller
 *
 * @author liweihai
 * @email liweihai12345@sina.com
 * @date 2019-04-10 13:51:33
 */
@RestController
@RequestMapping("productcate")
public class ProductCateController {
    @Autowired
    private ProductCateService productCateService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("productcate:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<ProductCateEntity> tProductCateList = productCateService.queryList(query);
        int total = productCateService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(tProductCateList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{productId}")
    @RequiresPermissions("productcate:info")
    public R info(@PathVariable("productId") Long productId) {
        ProductCateEntity tProductCate = productCateService.queryObject(productId);

        return R.ok().put("tProductCate", tProductCate);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("productcate:save")
    public R save(@RequestBody ProductCateEntity tProductCate) {

        productCateService.save(tProductCate);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("productcate:update")
    public R update(@RequestBody ProductCateEntity tProductCate) {
        //清除缓存
        J2CacheUtils.remove(Constants.REDIS_PRO_CATE_KEY);
        productCateService.update(tProductCate);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("productcate:delete")
    public R delete(@RequestBody Long[] productIds) {
        //清除缓存
        J2CacheUtils.remove(Constants.REDIS_PRO_CATE_KEY);
        productCateService.deleteBatch(productIds);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {
        //清除缓存
        J2CacheUtils.remove(Constants.REDIS_PRO_CATE_KEY);
        List<ProductCateEntity> list = productCateService.queryList(params);

        return R.ok().put("list", list);
    }
}
